package test;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import jodd.http.HttpRequest;
import jodd.http.HttpResponse;
import junit.framework.TestCase;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class MrTestCase extends TestCase {

    @Before
    public void setUp() throws Exception {

    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void testGet() {
        String url = "http://localhost:8089/zhongchuang/telecom";
        HttpRequest req = HttpRequest.get(url);
        HttpResponse resp = req.send();
        System.out.print(resp.body());
        resp.close();
    }

    @Test
    public void testPost() {
        String url = "http://218.244.158.72:9100/mr/list";
        HttpRequest req = HttpRequest.post(url).form("user", "ooxx");
        HttpResponse resp = req.send();
        System.out.print(resp);
        resp.close();
    }

    @Test
    public void testJzMM() {
        String province = "";
        String linkId = "qeur8y9q73wyr98217r";
        String port = "2364123";
        String msg = "283745628734";
        String orderAt = "27348723847r98234";
        String data = "123748123749123";
    }

    @Test
    public void testLzMM() {

        String post = "http://localhost:8080/lz/mr/baoyue";
        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><request><userId>1527931163</userId><contentId>617816016901</contentId><consumeCode>006043486001</consumeCode><cpid>795178</cpid><hRet>0</hRet><versionId>2_0_0</versionId><cpparam>M04123456789</cpparam><packageID /></request>";
        String str = "{\"Phone\":\"1244473748|100\",\"PayStatus\":\"1\",\"MoKey\":\"LZHL#BY8hzzx*123\",\"Money\":\"8.00\",\"OrderID\":\"B1004k3313t9u3z1u1kq513zbk1s77\"," +
        		"\"Sign\":\"A8757A47B16A4C46B3EDD0A2B982E396\",\"DestNum\":\"\",\"FeeTime\":\"2014-11-07 10:12:29\",\"Version\":\"2.0\",\"ChannelType\":\"SyncUmpSubStatus\",\"PayStatusCode\":\"0000\"}";
//        String baseXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><request><userId>132631679</userId><contentId>606710071600</contentId>"
//                + "<consumeCode>000071599001</consumeCode><cpid>701067</cpid><hRet>0</hRet><status>1800</status><versionId>21008</versionId>"
//                + "<cpparam>Y1004QDC2Q0Q5879</cpparam><packageID /></request>";
        try {
            // Post请求的url，与get不同的是不需要带参数
            URL postUrl = new URL(post);
            // 打开连接
            HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestMethod("POST");
            connection.setUseCaches(false);
            connection.setInstanceFollowRedirects(true);
            // 进行编码
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.connect();
            DataOutputStream out = new DataOutputStream(connection.getOutputStream());
            out.writeBytes(str);
            out.flush();
            out.close();
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));// 设置编码,否则中文乱码
            String line = "";
            System.out.println("=============================");
            System.out.println("Contents of post request");
            System.out.println("=============================");
            while ((line = reader.readLine()) != null) {

                System.out.println(line);
            }
            System.out.println("=============================");
            System.out.println("Contents of post request ends");
            System.out.println("=============================");
            reader.close();
            connection.disconnect();

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Test
    public void testLzMr() {
        String url = "http://localhost:8089/lz/mr?merId=5984&goodsId=150&orderId=L95FT7lmbf7s1vU3fp3z5993djgp39&goodsInf=150&merDate=20140515&payDate=20140515&amount=1500&amtType=02&bankType=3&mobileId=15732834512&transType=0&settleDate=20140515&merPriv=&retCode=0000&version=3.0";
        HttpRequest req = HttpRequest.get(url);
        HttpResponse resp = req.send();
        System.out.print(resp.body());
        resp.close();

    }

    @Test
    public void testSiBaiKeMr() {
        String url = "http://localhost:8089/sbk/mr/iosNetGame";
        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><request><userId>51081858</userId><contentId>602916013345</contentId>"
                + "<consumeCode>006035361019</consumeCode><cpid>741029</cpid><hRet>1</hRet><status>127</status><versionId>29015</versionId>"
                + "<cpparam>2014093064646248</cpparam><packageID /></request>";

        String xml2 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><request><userId>1068461728</userId><contentId>644016013261</contentId>"
                + "<consumeCode>006035092019</consumeCode><cpid>710440</cpid><hRet>1</hRet><versionId>100</versionId>"
                + "<cpParam>0000000000000000</cpParam><packageID /> </request>";
        try {
            // Post请求的url，与get不同的是不需要带参数
            URL postUrl = new URL(url);
            // 打开连接
            HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestMethod("POST");
            connection.setUseCaches(false);
            connection.setInstanceFollowRedirects(true);
            // 进行编码
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.connect();
            DataOutputStream out = new DataOutputStream(connection.getOutputStream());
            out.writeBytes(xml2);
            out.flush();
            out.close();
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));// 设置编码,否则中文乱码
            String line = "";
            System.out.println("=============================");
            System.out.println("Contents of post request");
            System.out.println("=============================");
            while ((line = reader.readLine()) != null) {

                System.out.println(line);
            }
            System.out.println("=============================");
            System.out.println("Contents of post request ends");
            System.out.println("=============================");
            reader.close();
            connection.disconnect();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
